﻿

/*
 * Test Script:
 * F1 - Heal Player / Give Armour / Fix Car
 * F3 - Remove Wanted Level
 * F5 - Spawn Banshee
 * F9 - Give Weapons 1
 * F10 - Give Weapons 2
 * 
 * MMB - Jump Vehicle
 * RMB - Accelerate Vehicle
 * End - Tow Vehicle
 * 
 * 0 - All peds in area jump in air.
 * 9 - All vehicles in area jump in air.
 * 8 - All objects in area jump in air.
 * 
 */






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTA;
using System.Windows.Forms;

namespace TestScript
{
    
    public class TestScript : ScriptBase
    {
        VehicleWeaponsForm frm;
        TestAim Aimer;
        bool isCtrlHeld = false;
        float timescale = 1.0F;
        
        public TestScript()
        {
            new Log("TestScript", true);
            Log.Debug("TestScript Constructor");
            Aimer = new TestAim();
            GTA.Events.OnStart.Handler += OnStart;
            GTA.Events.RunTick.Handler += RunTick;
            GTA.Events.KeyDown.Handler += OnKeyDown;
            GTA.Events.KeyUp.Handler += OnKeyUp;
        }

        public void OnStart(Object sender, EventArgs args)
        {
            Log.Debug("TestScript OnStart");
            //frm = new VehicleWeaponsForm();
            //GTA.Screen.RemoveForm(frm);
            //frm.Left = 10;
            //frm.Top = 10;
            //frm.UseMouse = true;
            //frm.Visible = true;
            Log.Debug("TestScript OnStart done");
        }

        public void RunTick(Object sender, EventArgs args)
        {
            //DrawForm(frm);
            //DrawMouse();

            GTA.ScriptApi.AlterWantedLevel(new GTA.Player(),0);
            GTA.ScriptApi.ApplyWantedLevelChangeNow(new GTA.Player());
        }

        public void OnKeyUp(Object sender, GTA.Events.KeyEventArgs args)
        {
            if (args.KeyCode == GTA.Keys.ControlKey) isCtrlHeld = false;
        }

        public void OnKeyDown(Object sender, GTA.Events.KeyEventArgs args)
        {
            Log.Debug("TestScript onKeyDown: " + args.KeyCode.ToString());
            //CheckMouseClick(KeyCode);
            if (args.KeyCode == GTA.Keys.ControlKey) isCtrlHeld = true;
            if (isCtrlHeld)
            {
                if (args.KeyCode == GTA.Keys.Subtract) 
                {
                    GTA.Screen.PrintString("Setting timescale: " + timescale.ToString(),2000);
                    timescale -= 0.1F; 
                    GTA.ScriptApi.SetTimeScale(timescale); 
                }
                if (args.KeyCode == GTA.Keys.Add) 
                {
                    GTA.Screen.PrintString("Setting timescale: " + timescale.ToString(),2000);
                    timescale += 0.1F; 
                    GTA.ScriptApi.SetTimeScale(timescale); 
                }
            }


            //if (args.KeyCode == GTA.Keys.P) Aimer.AttachAim();
            //if (args.KeyCode == GTA.Keys.O) Aimer.DetachAim();

            if (args.KeyCode == GTA.Keys.F1) HealPlayer();
            if (args.KeyCode == GTA.Keys.F3) ResetWanted();
            if (args.KeyCode == GTA.Keys.F5) SpawnVehicle(GTA.eModel.MODEL_BANSHEE);
            if (args.KeyCode == GTA.Keys.F7) GiveCash(1000);
            if (args.KeyCode == GTA.Keys.F8) WriteCoords();
            if (args.KeyCode == GTA.Keys.F9) GiveWeapons();
            if (args.KeyCode == GTA.Keys.F10) GiveWeapons2();

            if (args.KeyCode == GTA.Keys.RButton) AccelerateVehicle();
            if (args.KeyCode == GTA.Keys.MButton) JumpVehicle();

            if (args.KeyCode == GTA.Keys.End) TowVehicle();

            if (args.KeyCode == GTA.Keys.D0) JumpAllPedsInArea();
            if (args.KeyCode == GTA.Keys.D9) JumpAllVehiclesInArea();
            if (args.KeyCode == GTA.Keys.D8) JumpAllObjsInArea();

            if (args.KeyCode == GTA.Keys.F6)
            {
                Player pl = new GTA.Player();
                Vector3 plpos = pl.GetPlayerPed().Coordinates;
                GTA.Vehicle veh = new GTA.Vehicle();
                Log.Message("Vehicle create");
                GTA.eModel scrmodel = GTA.eModel.MODEL_BENSON;
                GTA.ScriptApi.RequestModel(scrmodel);
			    for(int i=0;i<=1000;i++)
			    {
				    if (GTA.ScriptApi.HasModelLoaded(scrmodel)) break;
                    ScriptApi.RequestModel(scrmodel);
			    }
                if (!ScriptApi.HasModelLoaded(scrmodel)) 
			    {
				    Log.Error("Unable to load vehicle model!");
                    ScriptApi.MarkModelAsNoLongerNeeded(scrmodel);
				    return;
			    }


                GTA.ScriptApi.CreateCar((uint)scrmodel, plpos.X, plpos.Y, plpos.Z, veh, true);
                Log.Message("Vehicle created1: "+veh.Handle);
                GTA.ScriptApi.DeleteCar(veh);
                Log.Message("Vehicle deleted1: " + veh.Handle);
            }

            //if (args.KeyCode == GTA.Keys.K) ClearArea();

        }




        // ------------------ some helper functions ------------------------

        unsafe public void WriteCoords()
        {
            Player pl = new Player();
            Ped plped = pl.GetPlayerPed();
            Vehicle plveh = plped.GetVehiclePedIsDriving();
            Vector3 pos;
            Log.Message("---------- NEW POS --------------");
            if (!Vehicle.Exists(plveh))
            {
                pos = plped.Coordinates;
                Log.Message("Player Coordinates: " + pos.X + "F, " + pos.Y + "F, " + pos.Z + "F");
                Log.Message("Player Heading: " + plped.Heading);
                
            }
            else
            {
                pos = plveh.Coordinates;
                Log.Message("Vehicle Coordinates: " + pos.X + "F, " + pos.Y + "F, " + pos.Z + "F");
                Log.Message("Vehicle Heading: " + plveh.Heading);
                Log.Message("Vehicle Rotation Quat: " + plveh.Quaternion.W + "F, " + plveh.Quaternion.X + "F, " + plveh.Quaternion.Y + "F, " + plveh.Quaternion.Z + "F");
            }
            GTA.Camera gamecam = new Camera();
            GTA.ScriptApi.GetGameCam(gamecam);
            float camx,camy,camz;
            GTA.ScriptApi.GetCamPos(gamecam, &camx, &camy, &camz);
            Log.Message("Camera Coordinates: " + camx + "F, " + camy + "F, " + camz + "F");
            float rotx, roty, rotz;
            GTA.ScriptApi.GetCamRot(gamecam, &rotx, &roty, &rotz);
            Log.Message("Camera Rotation: " + rotx + "F, " + roty + "F, " + rotz+"F");
            Log.Message("---------- END POS --------------");
            GTA.Screen.PrintString("Coordinates Written!", 3000);
        }

        unsafe public void GiveCash(uint givecash)
        {
            Player pl = new Player();
            Ped plped = pl.GetPlayerPed();
            uint cash = 0;
            GTA.ScriptApi.StoreScore(pl, &cash);
            //uint cash = ScriptApi.GetCharMoney(plped);
            //cash += givecash;
            //ScriptApi.SetCharMoney(plped,cash);
            GTA.Screen.PrintString("$" + givecash.ToString() + " Cash given! Total cash: "+ (cash+givecash).ToString(),3000);
            GTA.ScriptApi.AddScore(pl, (int)givecash);
        }

        unsafe public void HealPlayer()
        {
            Log.Debug("TestScript Heal Player");
            GTA.Screen.PrintString("Heal Player", 1000);
            Player pl = new Player();
            Ped plped = pl.GetPlayerPed();
            ScriptApi.SetCharHealth(plped, 200); // health
            uint armour = 0; 
            ScriptApi.GetCharArmour(plped, &armour); // no need for 'fixed' statement here
            ScriptApi.AddArmourToChar(plped, 200 - armour); // armour
            
            Vehicle v = new Vehicle();
            ScriptApi.GetCarCharIsUsing(plped, v);
            if (!v.Exists()) return;
            Log.Debug("TestScript Repair Vehicle");
            ScriptApi.ExtinguishCarFire(v);
			ScriptApi.FixCar(v);
			for (int i=0;i<4;i++)
			{
				ScriptApi.FixCarTyre(v, (GTA.eVehicleTyre)i);
			}
			ScriptApi.SetCarHealth(v,500);
			ScriptApi.SetEngineHealth(v,500);
        }

        public void ResetWanted()
        {
            Log.Debug("TestScript Reset Wanted");
            GTA.Screen.PrintString("Reset Wanted", 1000);
            GTA.Player pl = new Player();
            ScriptApi.AlterWantedLevel(pl, 0);
            ScriptApi.ApplyWantedLevelChangeNow(pl);
        }

        public void GiveWeapons()
        {
            Log.Debug("TestScript Give Weapons");
            GTA.Screen.PrintString("Give Weapons 1", 1000);
            GTA.Player pl = new Player();
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_M4, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_PISTOL, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_MP5, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_MINIGUN, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_SHOTGUN, 999, false);
        }

        public void GiveWeapons2()
        {
            Log.Debug("TestScript Give Weapons");
            GTA.Screen.PrintString("Give Weapons 2", 1000);
            GTA.Player pl = new Player();
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_M4, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_DEAGLE, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_MICRO_UZI, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_RLAUNCHER, 999, false);
            ScriptApi.GiveWeaponToChar(pl.GetPlayerPed(), GTA.eWeapon.WEAPON_SHOTGUN, 999, false);
        }

        unsafe public void SpawnVehicle(GTA.eModel model)
        {
            Log.Debug("TestScript Spawn Vehicle");
            GTA.Screen.PrintString("Spawn vehicle", 1000);
            GTA.Player pl = new Player();
            Vector3 plpos = pl.Coordinates;
            plpos.Z += 0.7F;
            plpos.Y += 5.5F;
            GTA.Vehicle v = GTA.World.CreateVehicle(model, plpos);
            if (!Vehicle.Exists(v)) return;
            Vehicle currentvehicle = pl.GetPlayerPed().GetVehiclePedIsDriving();
            float heading, speed=0.0F;
            if (Vehicle.Exists(currentvehicle))
            {
                ScriptApi.GetCarHeading(currentvehicle, &heading);
                ScriptApi.GetCarSpeed(currentvehicle, &speed);
            }
            else
            {
                ScriptApi.GetCharHeading(pl.GetPlayerPed(), &heading);
            }
            ScriptApi.SetCarHeading(v, heading);
            ScriptApi.SetCarForwardSpeed(v, speed);
            ScriptApi.WarpCharIntoCar(pl.GetPlayerPed(), v);
        }
        
        public void AccelerateVehicle()
        {
            GTA.Player pl = new Player();
            Vehicle v = new Vehicle();
            ScriptApi.GetCarCharIsUsing(pl.GetPlayerPed(), v);
            if (!v.Exists()) return;
            Log.Debug("TestScript Accelerate Vehicle");
            ScriptApi.ApplyForceToCar(v,1,0.0F,5.0F,-0.1F,0.0F,0.0F,0.0F,1,1,1,1);

        }

        public void JumpVehicle()
        {
            GTA.Player pl = new Player();
            Vehicle v = new Vehicle();
            ScriptApi.GetCarCharIsUsing(pl.GetPlayerPed(), v);
            if (!v.Exists()) return;
            Log.Debug("TestScript Jump Vehicle");
            ScriptApi.ApplyForceToCar(v,1,0.0F,0.5F,5.0F,0.0F,0.0F,0.0F,1,1,1,1);

        }

        unsafe public void TowVehicle()
        {
            GTA.Player pl = new Player();
            Vehicle v = new Vehicle();
            ScriptApi.GetCarCharIsUsing(pl.GetPlayerPed(), v);
            if (!v.Exists()) return;
            Log.Debug("TestScript Tow Vehicle");
            GTA.Screen.PrintString("Tow Vehicle", 1000);
            if (ScriptApi.IsCarAttached(v))
			{
				ScriptApi.DetachCar(v);
				return;
			}
            float x, y, z;
            ScriptApi.GetCharCoordinates(pl.GetPlayerPed(), &x, &y, &z);
            Vehicle towv = ScriptApi.GetClosestCar(x,y,z,7.5F,false,70);
            if (!towv.Exists()) return;
            if (ScriptApi.IsCarAttached(towv))
			{
				ScriptApi.DetachCar(towv);
				return;
			}

            ScriptApi.SetCarAsMissionCar(towv);

			//AttachCarToCarPhysically(towv, v, false, 0, yoff, xoff, zoff, xbuff, ybuff, zbuff, xrotv1, yrotv1,0.0F,0.0F,0.0F);
            ScriptApi.AttachCarToCarPhysically(towv, v, false, 0, 0.0F, -5.3F, 0.8F, 0.01F, 1.0F, 1.0F, 0.1F, 0.0F, 0.0F, 0.0F, 0.0F);
            ScriptApi.AttachCarToCarPhysically(v, towv, false, 0, 0.0F, 5.3F, -0.8F, 0.001F, 0.1F, 0.1F, -0.1F, 0.0F, 0.0F, 0.0F, 0.0F);
			return;
        }

        public void JumpAllPedsInArea()
        {
            GTA.Player pl = new Player();
            Vector3 plpos = pl.Coordinates;
            Ped[] peds = World.GetAllPedsInArea(plpos, 50.0F);
            Log.Debug("Peds count returned from area: " + peds.Length);
            foreach (Ped ped in peds)
            {
                if (ped == null) continue;
                if (ped.Handle == 0) continue;
                if (!ped.Exists()) continue;
                try
                {
                    ScriptApi.ApplyForceToPed(ped, 3, 0.0F, 0.0F, 10.0F, 0.0F, 0.0F, 0.0F, 0, 0, 1, 1);
                }
                catch (Exception ex)
                {
                    Log.Debug(ex.Message);
                    continue;
                }
            }
        }

        public void JumpAllVehiclesInArea()
        {
            
            GTA.Player pl = new Player();
            Vector3 plpos = pl.Coordinates;
            Vehicle[] vehicles = World.GetAllVehiclesInArea(plpos, 50.0F);
            Log.Debug("Vehicles count returned from area: " + vehicles.Length);
            foreach (Vehicle vehicle in vehicles)
            {
                if (vehicle == null) continue;
                if (vehicle.Handle == 0) continue;
                if (!vehicle.Exists()) continue;
                try
                {
                    ScriptApi.ApplyForceToCar(vehicle, 3, 0.0F, 0.0F, 20.0F, 0.0F, 0.0F, 0.0F, 0, 0, 1, 1);
                }
                catch (Exception ex)
                {
                    Log.Debug(ex.Message);
                    continue;
                }
            }
             
        }

        public void JumpAllObjsInArea()
        {
            
            GTA.Player pl = new Player();
            Vector3 plpos = pl.Coordinates;
            GameObject[] objs = World.GetAllObjectsInArea(plpos, 50.0F);
            Log.Debug("Objs count returned from area: " + objs.Length);
            foreach (GameObject obj in objs)
            {
                if (obj == null) continue;
                if (obj.Handle == 0) continue;
                if (!obj.Exists()) continue;
                try
                {
                    ScriptApi.SetObjectDynamic(obj, true);
                    ScriptApi.ApplyForceToObject(obj, 3, 0.0F, 0.0F, 10.0F, 0.0F, 0.0F, 0.0F, 0, 0, 1, 1);
                }
                catch (Exception ex)
                {
                    Log.Debug(ex.Message);
                    continue;
                }
            }
        }





        public void ClearArea()
        {
            GTA.Player pl = new Player();
            Vector3 plpos = pl.Coordinates;

            ScriptApi.ClearAreaOfCars(plpos.X, plpos.Y, plpos.Z, 500.0F);
            ScriptApi.ClearAreaOfChars(plpos.X, plpos.Y, plpos.Z, 500.0F);
            //ScriptApi.ClearArea((plpos.X, plpos.Y, plpos.Z, 500.0F,false);
            /*
            Vehicle[] vehicles = World.GetAllVehiclesInArea(plpos, 20.0F);
            Log.Debug("Vehicles count returned from area: " + vehicles.Length);
            foreach (Vehicle vehicle in vehicles)
            {
                if (vehicle == null) continue;
                if (vehicle.Handle == 0) continue;
                if (!vehicle.Exists()) continue;
                try
                {
                    
                    //ScriptApi.SetCarAsMissionCar(vehicle);
                    Log.Debug("deletevehicle mark: " + vehicle.Handle);
                    ScriptApi.DeleteCar(vehicle);
                    //ScriptApi.MarkCarAsNoLongerNeeded(vehicle);
                    Log.Debug("deletevehicle mark2: " + vehicle.Handle);
                }
                catch (Exception ex)
                {
                    Log.Debug(ex.Message);
                    continue;
                }
                Log.Debug("deletevehicle done: " + vehicle.Handle);
            }
             * */
        }




        // ------------ FORM -------------

        //GTAForm SelectedForm;
        //Control SelectedControl;

        unsafe GTAForm GetFormFromScreenXY(float x, float y)
        {
            uint resx, resy;
            ScriptApi.GetScreenResolution(&resx, &resy);
            uint px, py;
            px = (uint)((x / 1.0F) * (float)resx);
            py = (uint)((y / 1.0F) * (float)resy);

            //foreach (GTAForm^ frm in Forms)
            //{
            //if (frm == null) continue;
            if (((px > frm.Left) && (px < frm.Left + frm.Width)) && ((py > frm.Top) && (py < frm.Top + frm.Height))) return frm;
            //}
            return null;
        }

        unsafe Control GetControlFromScreenXY(GTAForm form, float x, float y)
        {
            uint resx, resy;
            ScriptApi.GetScreenResolution(&resx, &resy);
            uint px, py;
            px = (uint)((x / 1.0F) * (float)resx);
            py = (uint)((y / 1.0F) * (float)resy);

            foreach (Control ctrl in form.Controls)
            {
                if (ctrl == null) continue;
                if ((px > (form.Left + ctrl.Left)) && (px < (form.Left + (ctrl.Left + ctrl.Width))))
                { // x ok
                    if ((py > (form.Top + ctrl.Top)) && (py < (form.Top + (ctrl.Top + ctrl.Height))))
                    { // y ok
                        return ctrl;
                    }
                }
            }
            return null;
        }

        unsafe void CheckMouseClick(short KeyCode)
        {

            float mx, my;
            ScriptApi.GetMousePosition(&mx, &my);
            if (KeyCode == (short)GTA.Keys.LButton)
            {
                GTAForm foundfrm = GetFormFromScreenXY(mx, my);

                if (foundfrm != null)
                {
                    if (frm.UseMouse)
                    {
                        Log.Debug("Clicked in form: " + foundfrm.Name);
                        Control foundctrl = GetControlFromScreenXY(foundfrm, mx, my);
                        if (foundctrl != null)
                        {
                            Log.Debug("Clicked in control: " + foundctrl.Name);
                            if (foundctrl.GetType().Equals(typeof(Button)))
                            {
                                Log.Debug("its a button");
                                Button btn = (Button)foundctrl;

                                object[] args = new object[1];
                                args[0] = EventArgs.Empty;
                                System.Reflection.MethodInfo m = typeof(Button).GetMethod("OnClick", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                                m.Invoke(btn, args);
                            }

                            if (foundctrl.GetType().Equals(typeof(CheckBox)))
                            {
                                Log.Message("its a checkbox");
                                CheckBox chk = (CheckBox)foundctrl;
                                chk.Checked = !chk.Checked;
                                /*
                                object[] args = new object[1];
                                args[0] = EventArgs.Empty;
                                System.Reflection.MethodInfo m = typeof(CheckBox).GetMethod("OnCheckedChanged", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                                m.Invoke(chk, args);
                                 * */
                            }
                        }
                    }
                }
            }
        }

        unsafe void DrawMouse()
        { // draw the mouse onscreen
            /*
            bool showmouse = false;
            foreach (GTAForm form in FormStartPosition)
            {
                if (form == null) continue;
                if (form.UseMouse)
                {
                    showmouse = true;
                    break;
                }
            }
            if (!showmouse) return;
             * */
            float mx, my;
            ScriptApi.GetMousePosition(&mx, &my);
            if (!ScriptApi.HasStreamedTxdLoaded("network"))
            {
                ScriptApi.RequestStreamedTxd("network", true);
                if (!ScriptApi.HasStreamedTxdLoaded("network"))
                {
                    Log.Error("World::DrawMouse: Texture not loaded!");
                    return;
                }
            }
            GTA.Texture mtext = GTA.ScriptApi.GetTextureFromStreamedTxd("network", "MOUSECURSOR");

            ScriptApi.DrawSprite(mtext, mx + 0.02F, my + 0.02F, 0.034F, 0.049F, 0.0F, 255, 255, 255, 255);
            //ScriptApi.MarkStreamedTxdAsNoLongerNeeded("network");
        }

        unsafe void DrawForm(GTAForm form)
        { // draws the form onto game screen
            //System::Drawing::Bitmap^ frmbmp = gcnew System::Drawing::Bitmap(form->Width,form->Height);
            //form->DrawToBitmap(frmbmp, System::Drawing::Rectangle(0,0,form->Width,form->Height));
            if (form == null) return;
            float x1, y1, x2, y2;
            uint resx, resy;
            ScriptApi.GetScreenResolution(&resx, &resy);
            x1 = (float)(((float)((float)form.Left + ((float)form.Width / 2.0F)) / (float)resx) * 1.0F);
            y1 = (float)(((float)((float)form.Top + ((float)form.Height / 2.0F)) / (float)resy) * 1.0F);
            x2 = (float)(((float)((float)form.Width) / (float)resx) * 1.0F);
            y2 = (float)(((float)((float)form.Height) / (float)resy) * 1.0F);
            byte r, g, b, a;
            r = form.BackColor.R; g = form.BackColor.G; b = form.BackColor.B; a = form.BackColor.A;
            if (a >= 255) a = 127;
            ScriptApi.DrawRect(x1, y1, x2, y2, r, g, b, a);
            foreach (Control ctrl in form.Controls)
            {
                DrawFormControl(form, ctrl);
            }

        }

        unsafe void DrawFormControl(Control parent, Control ctrl)
        { // draws the controls on the form on gamescreen
            float x1, y1, x2, y2;
            uint resx, resy;
            ScriptApi.GetScreenResolution(&resx, &resy);
            x1 = (((float)parent.Left + ((float)ctrl.Left + ((float)ctrl.Width / 2.0F))) / (float)resx) * 1.0F;
            y1 = (((float)parent.Top + ((float)ctrl.Top + ((float)ctrl.Height / 2.0F))) / (float)resy) * 1.0F;
            x2 = ((float)ctrl.Width / (float)resx) * 1.0F;
            y2 = ((float)ctrl.Height / (float)resy) * 1.0F;
            byte r, g, b, a;
            r = ctrl.BackColor.R; g = ctrl.BackColor.G; b = ctrl.BackColor.B; a = ctrl.BackColor.A;
            if (a >= 255) a = 127;

            // --TEXT--
            float texscalex, texscaley;
            texscalex = (float)ctrl.Font.Size / (float)(resx/20); // 35.0F
            texscaley = texscalex;
            float texx1, texy1;


            texx1 = x1 - ((((float)(ctrl.Width) / 2.0F) / (float)(resx)) * 1.0F); //lhs of control
            texy1 = y1 - ((((float)(ctrl.Height) / 2.0F) / (float)(resx)) * 1.0F); //top of control
            //texx1 += (safe_cast<float>(ctrl->Font->Size)*safe_cast<float>(ctrl->Width))/10000.0F;
            texx1 += ((float)(ctrl.Font.Size) * (float)(ctrl.Width)) / 40000.0F; //500
            //texx1 += ((((float)(ctrl.Width) / 20.0F) / (float)(resx)) * 1.0F); // middle of control


            //texx1 = x1 - ((((float)ctrl.Width / 2.0F) / (float)resx) * 1.0F); //lhs of control
            //texy1 = y1 - ((((float)ctrl.Height / 2.0F) / (float)resx) * 1.0F); //top of control
            //texx1 += ((((float)ctrl.Width / 4.0F) / (float)resx) * 1.0F); // middle of control


            byte tr, tg, tb, ta;
            tr = ctrl.ForeColor.R; tg = ctrl.ForeColor.G; tb = ctrl.ForeColor.B; ta = ctrl.ForeColor.A;
            ScriptApi.SetTextColour(tr, tg, tb, ta);
            ScriptApi.SetTextScale(texscalex, texscaley);
            ScriptApi.SetTextDropshadow(false, 0, 0, 0, 0);
            // --/TEXT--

            if (ctrl.GetType().Equals(typeof(Button)))
            {
                Button button = (Button)ctrl;
                ScriptApi.DrawRect(x1, y1, x2 + 0.001F, y2 + 0.001F, (byte)(r + 10), (byte)(g + 10), (byte)(b + 10), 64); //outside
                ScriptApi.DrawRect(x1, y1, x2, y2, r, g, b, a);
                if (button.Text != "")
                {
                    ScriptApi.DisplayTextWithLiteralString(texx1, texy1, "STRING", button.Text);
                }
            }
            else if (ctrl.GetType().Equals(typeof(Label)))
            {
                Label lbl = (Label)ctrl;
                if (lbl.Text != "")
                {
                    texx1 -= texscalex / 10.0F;
                    ScriptApi.DisplayTextWithLiteralString(texx1, texy1, "STRING", lbl.Text);
                }
            }
            if (ctrl.GetType().Equals(typeof(CheckBox)))
            {
                CheckBox chk = (CheckBox)ctrl;
                x2 = 0.02F;
                y2 = 0.02F;
                ScriptApi.DrawRect(x1 - 0.04F, y1 - 0.01F, x2, y2, r, g, b, a);
                if (!chk.Checked) ScriptApi.DrawRect(x1 - 0.04F, y1 - 0.01F, x2 - 0.005F, y2 - 0.005F, 0, 0, 0, a);
                else ScriptApi.DrawRect(x1 - (0.04F + 0.01F), y1 - (0.01F + 0.01F), x2 - 0.02F, y2 - 0.02F, 0, 0, 0, a);
                ScriptApi.DisplayTextWithLiteralString(texx1 + 0.03F, texy1, "STRING", chk.Text);
            }
        }

        /**/




    }
     
}
